package com.daigoudao.web.common;

import java.util.Calendar;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.daigoudao.util.Constants;
import com.daigoudao.util.StringUtils;
import com.daigoudao.web.model.Cart;
import com.daigoudao.web.model.UserModel;
import com.danga.MemCached.MemCachedClient;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements SessionAware,
		ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 1L;

	protected static final String CERTIFICATION = "certification";

	protected static final String AJAX = "ajax";

	protected int id;

	protected String backUrl;

	protected String ajaxMsg;

	protected String forwardUrl;

	/**
	 * 请求类型：0：普通请求 1：ajax请求
	 */
	protected int requestType;

	private String q;

	private int area;

	private int category;

	private int pubtime;

	private int tag;

	private int o;

	protected int read = -1;
	
	protected int replyTag = -1;

	public String responseMsg;

	public HttpServletRequest request;

	public HttpServletResponse response;

	protected String action;

	@SuppressWarnings("rawtypes")
	public Map session;

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public int getArea() {
		return area;
	}

	public void setArea(int area) {
		this.area = area;
	}

	public int getCategory() {
		return category;
	}

	public void setCategory(int category) {
		this.category = category;
	}

	public int getPubtime() {
		return pubtime;
	}

	public void setPubtime(int pubtime) {
		this.pubtime = pubtime;
	}

	public int getTag() {
		return tag;
	}

	public void setTag(int tag) {
		this.tag = tag;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	/**
	 * 判断用户是否登录
	 * 
	 * @return
	 */
	public boolean isLogin() {
		UserModel userModel = (UserModel) session.get(Constants.USER_INFO);
		if (userModel == null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;

	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;

	}

	public String getResponseMsg() {
		return responseMsg;
	}

	public void setResponseMsg(String responseMsg) {
		this.responseMsg = responseMsg;
	}

	protected UserModel getUserLogin() {
		return (UserModel) session.get(Constants.USER_INFO);
	}

	protected String processPubtime() {
		Calendar c = Calendar.getInstance();
		switch (pubtime) {
		case 1:
			c.add(Calendar.DAY_OF_MONTH, -7);
			break;
		case 2:
			c.add(Calendar.MONTH, -1);
			break;
		case 3:
			c.add(Calendar.MONTH, -3);
			break;
		}
		return StringUtils.formatDate(c.getTime(), "yyyy-MM-dd");
	}

	public String getBackUrl() {
		return backUrl;
	}

	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}

	public String getAjaxMsg() {
		return ajaxMsg;
	}

	public void setAjaxMsg(String ajaxMsg) {
		this.ajaxMsg = ajaxMsg;
	}

	public int getRequestType() {
		return requestType;
	}

	public void setRequestType(int requestType) {
		this.requestType = requestType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	protected void addCart(MemCachedClient memcachedClient, Cart cart,
			int userId) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 7);
		memcachedClient.set(Constants.CART + "_" + userId, cart,
				calendar.getTime());
	}

	protected Cart getCart(MemCachedClient memcachedClient, int userId) {
		Cart cart = (Cart) memcachedClient.get(Constants.CART + "_" + userId);
		if (cart == null) {
			cart = new Cart();
		}
		return cart;
	}

	public int getO() {
		return o;
	}

	public void setO(int o) {
		this.o = o;
	}

	public void setRead(int read) {
		this.read = read;
	}

	public int getRead() {
		return read;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getAction() {
		return action;
	}
	
	public int getReplyTag() {
		return replyTag;
	}
	
	public void setReplyTag(int replyTag) {
		this.replyTag = replyTag;
	}
}
